package com.fingard.net;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;

public class TcpReceiver {

    public byte[] recBytes;//reqBytes;
    
    public int contentLength ;
    public int contentStartIndex ;
   
    public int readedLength;
    public int receiveOffSet;

    public String sourceIP;
    public String connState;
    public String connMsg;
    public boolean isCompleted;
    /**获取contentLength之后的第一个\n的位置*/
    public int firstCutLn;
    
    public String getConnMsg(){
    	if(connMsg != null && connMsg.length() > 0){
			return connMsg;
		}
		else if(!isCompleted){
			return "报文未正常接收完成";
		}
		else{
			return "";
		}
    }
    
    /**获取收到的报文，去掉通讯头部字节*/
    public String getContentString(String pCharset) throws UnsupportedEncodingException{
    	if(readedLength>0){
    		return new String(recBytes, contentStartIndex, contentLength, pCharset);
    	}else{
    		return "";
    	}
    }
    
    /**获取收到的报文全文*/
    public String getFullString(String pCharset) throws UnsupportedEncodingException{
    	if(readedLength>0){
    		return new String(recBytes, 0, readedLength, pCharset);
    	}else{
    		return "";
    	}
    }
    
    /**获取收到的全部字节*/
    public byte[] getFullBytes() throws UnsupportedEncodingException{
    	if(readedLength>0){
    		byte[] bytesRet = new byte[readedLength];
            System.arraycopy(recBytes, 0, bytesRet, 0, readedLength);
            return bytesRet;
    	}else{
    		return new byte[0];
    	}
    }
    
    public TcpReceiver() {
    	//charsetName = p_charsetName;
    	reset();
	}
    /**读取通讯头部的offSet*/
    public int readHeadStartIndex;
    public boolean isCipherSocket;
    public String cipherKey;
    public String cipherIV;
    public String digestSign;
    
    public String action;
    public boolean isSyncFile;
    
    /**DSP服务端响应是否为文件字节*/
    public boolean isRtnBinaryFile;
    
    public String oriFilePath;
    public String trgFilePath;
    public String fileType;
    public String charsetName;
    public HashMap<String, String> headKV;
    public String hostRawString;
    
    public void reset()
    {
    	headKV = new HashMap<String, String>();
    	hostRawString = "";
    	contentLength = -1;
    	firstCutLn=-1;
    	contentStartIndex = 0;
    	readedLength = 0;
    	receiveOffSet=0;
    	connState = "";
    	connMsg = "";
    	recBytes = null;
    	readHeadStartIndex=0;
    	isCipherSocket = false;
    	isSyncFile = false;
    	isCompleted = false;
    	isRtnBinaryFile = false;
    }
}
